package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.EoExecutePeople;
import com.example.demo.mapper.ExecutePeopleMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/e-people")
public class ExecutePeopleController extends BaseController{

    @Resource
    ExecutePeopleMapper executePeopleMapper;

    //根据 EO单id 查询 保障人员
    @GetMapping("/list/{id}")
    public Result<?> list(@PathVariable Long id){
        List<EoExecutePeople> people = executePeopleMapper.selectAllById(id);
        return Result.success(people);
    }

    //根据 人员id 查询 其保障任务
    @GetMapping("/listByPeopleId/{id}")
    public Result<?> listByPeopleId(@PathVariable Long id){
        List<EoExecutePeople> people = executePeopleMapper.selectAllByPeopleId(id);
        return Result.success(people);
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody EoExecutePeople people){
        return Result.success(executePeopleMapper.updateById(people));
    }

    @PostMapping("/confirm/{id}")
    public Result<?> confirm(@PathVariable Integer id){
        EoExecutePeople people = executePeopleMapper.selectById(id);
        people.setStatus("已确认");
        return Result.success(executePeopleMapper.updateById(people));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id){
        return Result.success(executePeopleMapper.deleteById(id));
    }

    @GetMapping("/select")
    public Result<?> select(@RequestParam Long eoId, @RequestParam Long peopleId){
        LambdaQueryWrapper<EoExecutePeople> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EoExecutePeople::getPeopleId, peopleId);
        wrapper.eq(EoExecutePeople::getEoId, eoId);
        return Result.success(executePeopleMapper.selectOne(wrapper));
    }
}
